import string
import string_operations as str_op
from document import *

FROMTAG = "From:"
TOTAG = "To:"
SUBJECTTAG = "Subject:"
CCTAG = "Cc:"
MIMETAG = "Mime-Version:"
ENDSUBTAG = "Content-Type:"
XFROMTAG = "X-From:"
XTOTAG = "X-To:"
XCCTAG = "X-cc:"
XBCCTAG = "X-bcc:"
XFOLDERTAG = "X-Folder:"
SEPARATOR = "\n\n"
ALL_TAGS = [FROMTAG, TOTAG, SUBJECTTAG, CCTAG, MIMETAG, ENDSUBTAG, XFROMTAG, XTOTAG, XCCTAG, XBCCTAG, XFOLDERTAG, SEPARATOR]

def parseMail(mail_content, new_doc):
    """
    Parse the mail header and the body. 
    
    Return the body as a parsed list
    """
    header, body = getHeaderAndBody(mail_content)
    parseMailHeader(header, new_doc)
    return str_op.parseString(body)

def getHeaderAndBody(mail_content):
    """
    mail_content - a string
    
    Return the tuple (header, body) of the mail content
    """
    part_list = mail_content.partition(SEPARATOR)
    return part_list[0], part_list[2]

def parseMailHeader(header, new_doc):
    """
    Process the title and authors. 
    
    A void method
    """
    header_list = createHeaderList(header)   
    getTitle(new_doc, header_list)
    processAuthors(new_doc, header_list)

def createHeaderList(header):
    """
    Make a header list delimited by spaces    
    """
    return header.split()

def processAuthors(new_doc, header_list):
    """
    Add author names and addresses into new_doc
    """
    getAuthorNames(new_doc, header_list)
    getAuthorAddresses(new_doc, header_list)

def getAuthorNames(new_doc, header_list):
    """
    Add the author names into new_doc's
    authors attribute
    
    A void method despite is "get" name
    """
    
    name_list = extractMultipleTagData(header_list, XFROMTAG, XTOTAG, XCCTAG)
    addNames(name_list, new_doc)
    
def extractMultipleTagData(header_list, fromtag, totag, cctag):
    from_list = []
    to_list = []
    cc_list = []
    if fromtag in header_list:
        from_list = retrieveTagData(header_list, fromtag)
    if totag in header_list:
        to_list = retrieveTagData(header_list, totag)
    if cctag in header_list:
        cc_list = retrieveTagData(header_list, cctag)
    return from_list + to_list + cc_list
    
def addNames(list, new_doc):
    names = ""
    if len(list) > 0:
        for word in list:
            names += word + " "
    new_doc.authors = str_op.formatString(names)
    
def getAuthorAddresses(new_doc, header_list):
    """
    Add the author addresses into new_doc's authors
    attribute
    
    Note: new_doc's input authors also get updated. 
    
    A void method despite its name. 
    """
    address_list = extractMultipleTagData(header_list, FROMTAG, TOTAG, CCTAG)
    addAddresses(address_list, new_doc)

def addAddresses(list, new_doc):
    if len(list) > 0:
        for word in list:
            new_doc.authors.append(word)
            new_doc.input_authors += word + " "
        
def retrieveTagData(header_list, tag):
    data_list = []
    data_begin = header_list.index(tag)
    data_end = header_list.index(findNextTag(tag, header_list))
    for index in range((data_begin) + 1, (data_end)):
        data_list.append(header_list[index])
    return data_list      
      
def getTitle(new_doc, header_list):
    """
    Add mail's title as a list into new_doc's title attribute.  
    
    Also update new_doc's input_title, or the unformatted title.
    
    A void method despite its name. 
    """
    
    title_list = retrieveTagData(header_list, SUBJECTTAG)
    title = ""
    if len(title_list) > 0:
        for word in title_list:
            title += word + " "
    new_doc.input_title = title
    new_doc.title = str_op.formatString(title)

def findNextTag(current_tag, header_list):
    """
    Return the next tag. 
    """
    begin_index = header_list.index(current_tag)
    for index in range(begin_index+1, len(header_list)):
        if header_list[index] in ALL_TAGS:
            return header_list[index]
    